#ifndef __INETADDR_HPP__
#define __INETADDR_HPP__
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
class InetAddr
{
    private:
        //初始化ip和port
        void GetAddress(std::string* ip , uint16_t* port)
        {
            *ip = inet_ntoa(_addr.sin_addr);
            *port = htons(_addr.sin_port);
        }
    public:
        //通过addr对象构造
        InetAddr(const struct sockaddr_in & addr) : _addr(addr)
        {
            GetAddress(&_ip,&_port);
        }
        //通过ip和port进行构造
        InetAddr(const std::string& ip , uint16_t port) : _ip(ip) , _port(port)
        {
            _addr.sin_addr.s_addr = inet_addr(_ip.c_str());
            _addr.sin_family = AF_INET;
            _addr.sin_port = htons(_port);
        }
        //无参构造
        InetAddr(){}
        //返回ip地址
        std::string Ip(){return _ip;}
        //返回端口号
        uint16_t Port(){return _port;}
        //返回addr对象
        struct sockaddr_in Addr(){return _addr;}
        //判断两个addr对象是否相当
        bool operator==(const InetAddr& addr)
        {
            return _ip == addr._ip && _port == addr._port;
        }
        InetAddr operator=(const InetAddr& addr)
        {
            _addr = addr._addr;
            _ip = addr._ip;
            _port = addr._port;
            return *this;
        }
    private:
        struct sockaddr_in _addr;
        std::string _ip;
        uint16_t _port;
};

#endif